package com.lwy.security;


import com.lwy.dao.BookUserMapper;
import com.lwy.entity.*;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class UserConfig implements UserDetailsService {

    @Resource
    private BookUserMapper bookUser;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        List<GrantedAuthority> grantedAuthorities=null;
        if(s.equals("游客")){
            MyBookUser myBookUser = new MyBookUser();
            myBookUser.setId(1);
            myBookUser.setUsername("游客");
            myBookUser.setEmail(null);
            myBookUser.setPassword(null);
            myBookUser.setSex(null);
            myBookUser.setProvince(null);
            myBookUser.setCity(null);
            myBookUser.setCountry(null);
            myBookUser.setHeadimgurl(null);
            myBookUser.setIntroduce(null);
            long s1 = 1;
            myBookUser.setStatus((byte) s1);
            myBookUser.setCreate_time(null);
            myBookUser.setUpdate_time(null);
            myBookUser.setIdentity(null);
            myBookUser.setTitle(null);
            myBookUser.setUp(null);
            myBookUser.setRecommend(null);
            myBookUser.setRoleInfos(null);
            myBookUser.setAuthorities(null);
            return myBookUser;
        }else{
            BookUser bookUser = this.bookUser.selectAll(s);
            List<RoleInfo> roleInfos = bookUser.getRoleInfos();
            for (RoleInfo roleInfo : roleInfos) {
                String name = roleInfo.getName();
                grantedAuthorities = AuthorityUtils.commaSeparatedStringToAuthorityList(name);
            }
            if(bookUser!=null){
                MyBookUser myBookUser = new MyBookUser();
                myBookUser.setId(bookUser.getId());
                myBookUser.setUsername(bookUser.getUsername());
                myBookUser.setEmail(bookUser.getEmail());
                myBookUser.setPassword(bookUser.getPassword());
                myBookUser.setSex(bookUser.getSex());
                myBookUser.setProvince(bookUser.getProvince());
                myBookUser.setCity(bookUser.getCity());
                myBookUser.setCountry(bookUser.getCountry());
                myBookUser.setHeadimgurl(bookUser.getHeadimgurl());
                myBookUser.setIntroduce(bookUser.getIntroduce());
                myBookUser.setStatus(bookUser.getStatus());
                myBookUser.setCreate_time(bookUser.getCreate_time());
                myBookUser.setUpdate_time(bookUser.getUpdate_time());
                myBookUser.setIdentity(bookUser.getIdentity());
                myBookUser.setTitle(bookUser.getTitle());
                myBookUser.setUp(bookUser.getUp());
                myBookUser.setRecommend(bookUser.getRecommend());
                myBookUser.setRoleInfos(bookUser.getRoleInfos());
                myBookUser.setAuthorities(grantedAuthorities);
                return myBookUser;
            }else{
                throw  new UsernameNotFoundException("账号密码不存在");
            }
        }

    }
}
